//
//  VisitorView.swift
//  SinaWeibo
//
//  Created by celeste on 16/5/14.
//  Copyright © 2016年 celeste. All rights reserved.
//

import UIKit
import SnapKit

protocol VisitorViewDelegate : NSObjectProtocol {
    
    //注册
    func visitorViewDidRegister()
    //登录
    func visitorViewDidLogin()
    
}
class VisitorView: UIView {
    
    // MARK : - 代理属性
    //代理一定不要忘记用weak
    weak var delegate : VisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        // 添加监听方法
        registerButton.addTarget(self, action: "clickRegisterButton", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "clickLoginButton", forControlEvents: .TouchUpInside)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK : - 监听方法
    
    @objc private func clickRegisterButton() {
        delegate?.visitorViewDidRegister()
    }
    
    @objc private func clickLoginButton() {
        delegate?.visitorViewDidLogin()
    }
    
    
    //设置访客视图信息
    func setupInfo(imageName: String? = nil,message: String) {
        
        messageLabel.text = message
        guard let imageName = imageName else {
            startAnimation()
            
            return
        }
        homeIconView.hidden = true
        maskIconView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    
    private func startAnimation() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.toValue = 2 * M_PI
        ani.duration = 20
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        iconView.layer.addAnimation(ani, forKey: nil)
    }
    
    // MARK : - 私有控件
    private lazy var iconView: UIImageView = UIImageView(ld_imageName: "visitordiscover_feed_image_smallicon")
    private lazy var maskIconView: UIImageView = UIImageView(ld_imageName: "visitordiscover_feed_mask_smallicon")
    private lazy var homeIconView: UIImageView = UIImageView(ld_imageName: "visitordiscover_feed_image_house")
    private lazy var messageLabel: UILabel = UILabel(ld_text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",alignment: .Center)
    lazy var registerButton: UIButton = UIButton(
        ld_title: "注册",
        titleColor: UIColor.orangeColor(),
        backgroundImageName: "common_button_white_disable")
     //登录按钮
    lazy var loginButton: UIButton = UIButton(
        ld_title: "登录",
        backgroundImageName: "common_button_white_disable")

}


    

// MARK : - 设置界面
extension VisitorView {
    
    private func setupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(30)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }

       //背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
}